package com.aos.erp.manager.rest;

import com.aos.erp.common.bean.PageBean;
import com.aos.erp.common.bean.ResultEntity;
import com.aos.erp.manager.dto.DsContractDTO;
import com.aos.erp.manager.model.dsc.DsContract;
import com.aos.erp.manager.model.dsc.vo.DsContractVo;
import com.aos.erp.manager.model.dsc.vo.DscConditionVo;
import com.aos.erp.manager.model.sys.dto.UserDto;
import com.aos.erp.manager.service.DsContractService;
import com.aos.erp.manager.util.UserUtil;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Slf4j
@Api(tags = {"UI接口：驾校合同接口"})
@RestController
@RequestMapping("/api/dsc")
public class RestDsContractController {

    @Resource
    private DsContractService dsContractService;

    @ApiOperation(value = "保存驾校合同")
    @ApiImplicitParams({
//            @ApiImplicitParam(name = "dscCode", value = "合同编号: HT+年月日时分秒+3位数字序号", required = true, dataType = "NoticeType"),
            @ApiImplicitParam(name = "dscName", value = "合同名称：100字符内", required = true, dataType = "String"),
            @ApiImplicitParam(name = "dscDsId", value = "合同驾校ID", dataType = "Integer"),
            @ApiImplicitParam(name = "dscDsCode", value = "驾校编号", dataType = "String"),
            @ApiImplicitParam(name = "dscDsName", value = "驾校名称", dataType = "String"),
            @ApiImplicitParam(name = "dscCustomerName", value = "客户名称：100字符内", required = true, dataType = "String"),
            @ApiImplicitParam(name = "dscContactsName", value = "联系人：100字符内", required = true, dataType = "String"),
            @ApiImplicitParam(name = "dscContactsPhone", value = "联系电话", required = true, dataType = "String"),
            @ApiImplicitParam(name = "dscSignName", value = "签订人：100字符内", required = true, dataType = "String"),
            @ApiImplicitParam(name = "dscSignTime", value = "签订时间", required = true, dataType = "String"),
            @ApiImplicitParam(name = "dscBeginTime", value = "合同开始时间", required = true, dataType = "String"),
            @ApiImplicitParam(name = "dscEndTime", value = "合同截止时间", required = true, dataType = "String"),
            @ApiImplicitParam(name = "dscFileList", value = "驾校合同文件[{dscFileId:1111,dscFileType:'picture'}]", required = true, dataType = "ArrayList"),
    })
    @PostMapping("/add")
    public ResultEntity addDsc(@ApiParam("入参") @RequestBody DsContractVo params, String currentUserId) throws Exception{

        UserDto userDto = UserUtil.getCurrentUserInfo(currentUserId);
        params.setOptUser(userDto.getName());

        dsContractService.saveDsc(params);
        return ResultEntity.success("新增成功");
    }

    @ApiOperation(value = "编辑驾校合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "合同ID", required = true, dataType = "Long"),
            @ApiImplicitParam(name = "dscName", value = "合同名称：100字符内", required = true, dataType = "String"),
            @ApiImplicitParam(name = "dscDsId", value = "合同驾校ID", dataType = "Integer"),
//            @ApiImplicitParam(name = "dscDsCode", value = "驾校编号", dataType = "String"),
            @ApiImplicitParam(name = "dscDsName", value = "驾校名称", dataType = "String"),
            @ApiImplicitParam(name = "dscCustomerName", value = "客户名称", required = true, dataType = "String"),
            @ApiImplicitParam(name = "dscContactsName", value = "联系人", required = true, dataType = "String"),
            @ApiImplicitParam(name = "dscContactsPhone", value = "联系电话", required = true, dataType = "String"),
            @ApiImplicitParam(name = "dscSignName", value = "签订人", required = true, dataType = "String"),
            @ApiImplicitParam(name = "dscSignTime", value = "签订时间", required = true, dataType = "String"),
            @ApiImplicitParam(name = "dscBeginTime", value = "合同开始时间", required = true, dataType = "String"),
            @ApiImplicitParam(name = "dscEndTime", value = "合同截止时间", required = true, dataType = "String"),
            @ApiImplicitParam(name = "dscFileList", value = "驾校合同文件[{dscFileId:1111,dscFileType:'picture'}]", required = true, dataType = "ArrayList"),
    })
    @PostMapping("/update")
    public ResultEntity updateDsc(@ApiParam(value = "入参") @RequestBody DsContractVo params, String currentUserId) throws Exception {

        UserDto userDto = UserUtil.getCurrentUserInfo(currentUserId);
        params.setOptUser(userDto.getName());

        dsContractService.updateDsc(params);
        return ResultEntity.success("修改成功");
    }

    @ApiOperation(value = "删除驾校合同")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "合同ID：id1,id2,id3", required = true, defaultValue = "", dataType = "String", paramType = "Query")
    })
    @GetMapping("/delete")
    public ResultEntity deleteDsc(String ids) throws Exception {
        dsContractService.deleteDsc(ids);
        return ResultEntity.success("删除成功");
    }


    @ApiOperation(value = "查看合同详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "dataId", value = "合同ID", required = true, dataType = "Long", paramType = "Query")
    })
    @GetMapping("/detail")
    public ResultEntity<DsContractDTO> detailNotice(Long dataId){
        return ResultEntity.success(dsContractService.getDscDetail(dataId));
    }

    @GetMapping("/list")
    @ApiOperation(value = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageIndex", value = "当前页", required = true, dataType = "Integer", defaultValue = "1", paramType = "Query"),
            @ApiImplicitParam(name = "pageSize", value = "每页大小", required = true, dataType = "Integer", defaultValue = "20", paramType = "Query"),
            @ApiImplicitParam(name = "dscCode", value = "合同编号", dataType = "String"),
            @ApiImplicitParam(name = "dscName", value = "合同名称", dataType = "String"),
            @ApiImplicitParam(name = "dscCustomerName", value = "客户名称", dataType = "String"),
            @ApiImplicitParam(name = "dscSignName", value = "签订人", dataType = "String"),
            @ApiImplicitParam(name = "timeType", value = "合同时间类型：dsc_start 为 合同开始时间；dsc_end 为 合同截止时间", dataType = "DscTimeType"),
            @ApiImplicitParam(name = "fromTime", value = "开始时间", dataType = "String"),
            @ApiImplicitParam(name = "toTime", value = "截止时间", dataType = "String"),
    })
    public ResultEntity<PageBean<DsContract>> queryPageBean(DscConditionVo params) {
        return ResultEntity.success(dsContractService.findByPage(params));
    }
}